package org.jim.server.command.handler.group;

import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.exception.ImException;
import org.jim.core.packets.*;
import org.jim.core.protocol.IProtocol;
import org.jim.core.utils.JsonKit;
import org.jim.server.ImServerChannelContext;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.processor.group.GroupCmdProcessor;
import org.jim.server.protocol.ProtocolManager;
import org.jim.server.req.ImGroupReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 创建群组指令
 */
public class CreateGroupHandler extends AbstractCmdHandler {

    private static final Logger log = LoggerFactory.getLogger(CreateGroupHandler.class);

    @Override
    public Command command() {
       return Command.COMMAND_CREATE_GROUP_REQ;
    }

    @Override
    public ImPacket handler(ImPacket imPacket, ImChannelContext imChannelContext) throws ImException {

        // 记录登录消息命令操作的日志信息
        log.info("【创建群组命令处理器】操作");

        ImServerChannelContext imServerChannelContext = (ImServerChannelContext)imChannelContext;

        ImGroupReq groupReq = JsonKit.toBean(imPacket.getBody(), ImGroupReq.class);

        // 获取登录命令处理器实例
        GroupCmdProcessor groupCmdProcessor = this.getSingleProcessor(GroupCmdProcessor.class);

        // 创建一个成功的登录响应体
        RespBody resp = new RespBody();

        resp.setCommand(Command.COMMAND_CREATE_GROUP_RESP);

        /**
         * 通过处理器处理创建群组操作
         */
        groupCmdProcessor.createGroup(resp,groupReq,imChannelContext);

        // 获取当前连接使用的协议类型
        IProtocol protocol = imServerChannelContext.getProtocolHandler().getProtocol();

        return ProtocolManager.Converter.respPacket(resp, imChannelContext);
    }

}
